﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;

namespace WaiGaoQiaoWeb.Controllers
{
    public class PersonCardController : Controller
    {
        private PersonCardInfo perinfo = new PersonCardInfo(true);
        public ActionResult PersonCardIndex()
        {
            ViewData["deptName"] = GetdeptName();
            ViewData["GroupDept"] = GetGroupDept("-1");
            return View();
        }

        /// <summary>
        /// 获取班组列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetGroupDept(string depid)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = DeptDDLInfo.GetGroupDept(depid);
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["groupid"]), ds.Tables[0].Rows[i]["groupname"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public ActionResult GetGroupDeptChange(string depid)
        {
            ViewData["GroupDept"] = GetGroupDept(depid);
            return Json(ViewData["GroupDept"]);
        }

        /// <summary>
        /// 获取未发卡人员列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetPersonCardList(PageVM pvm)
        {

            string query = string.Empty;
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                string strDep_ID = "";
                string strGroup_ID = "";
                string strEmpNo = "";
                if (param[2] != "-1")
                {
                    strDep_ID += " and Dep_ID='" + param[2] + "'";
                }
                else
                {
                    strDep_ID += " and 1=1 ";
                }
                if (param[3] != "-1")
                {
                    strGroup_ID += " and GroupIdDept='" + param[3] + "'";
                }
                else
                {
                    strDep_ID += " and 1=1 ";
                }
                if (param[0] != "")
                {
                    strEmpNo += " and EmpNo='" + param[0] + "'";
                }
                else
                {
                    strEmpNo += " and 1=1 ";
                }
                query = strEmpNo + " and PName like '%" + param[1] + "%'" + strDep_ID + strGroup_ID;
            }
            IList<PersonCardInfo> perlist = perinfo.SelPersonCardInfo(pvm.rp, pvm.page, query);

            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = perinfo.GetPersonCardCountWhere(query);
            List<string> v;
            foreach (PersonCardInfo item in perlist)
            {
                #region
                v = new List<string>();
                v.Add(item.PID.ToString());
                v.Add(item.EmpNo);
                v.Add(item.PName);
                v.Add(item.deptName);
                v.Add(item.PersonTypeName);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.PID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetdeptName()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = perinfo.GetdeptName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetDept()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = perinfo.GetDept();
            dic.Add(-1, "请选择");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 点击发卡按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult GrantCard(string id)
        {
            PersonCardVM vm = new PersonCardVM();
            PersonCardInfo info = new PersonCardInfo();
            perinfo = info.GetPersonCardInfoOne(id);
            vm.PID = perinfo.PID;
            vm.EmpNo = perinfo.EmpNo;
            vm.deptName = perinfo.deptName;
            vm.PersonTypeName = perinfo.PersonTypeName;
            vm.PName = perinfo.PName;
            vm.Dep_ID = perinfo.Dep_ID;
            //vm.InureTime = Convert.ToDateTime(Convert.ToDateTime("2010-01-01").ToString("yyyy-MM-dd"));
            //vm.ExpiredTime = Convert.ToDateTime("2099-01-01");
            return View(vm);
        }

        /// <summary>
        /// 点击发临时卡按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult GrantCardTemp()
        {
            ViewData["dept"] = GetDept();
            PersonCardTempVM pvm = new PersonCardTempVM();
            pvm.CardOptDeposit = 0;
            pvm.Balance = 0;
            return View(pvm);
        }

        /// <summary>
        /// 保存（给人员发卡）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveGrantCard(PersonCardVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                PersonCardInfo per = new PersonCardInfo()
                {
                    EmpNo = model.EmpNo,
                    CardSerial = Convert.ToInt64(model.CardSerial),
                    CardOptDeposit = Convert.ToInt32(model.CardOptDeposit * 100),
                    InureTime = model.InureTime,
                    ExpiredTime = model.ExpiredTime,
                    Balance = Convert.ToInt32(model.Balance * 100),
                    OperatorID = ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName,
                    blSubsidyCard = model.blSubsidyCard
                };
                result = perinfo.SaveGrantCard(per);
            }
            return Json(result);
        }

        /// <summary>
        /// 保存（给临时人员发卡）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveGrantCardTemp(PersonCardTempVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                PersonCardInfo per = new PersonCardInfo()
                {
                    EmpNo = model.EmpNo,
                    Dep_ID = model.Dep_ID,
                    PName = model.PName,
                    CardSerial = Convert.ToInt64(model.CardSerial),
                    CardOptDeposit = Convert.ToInt32(model.CardOptDeposit * 100),
                    InureTime = model.InureTime,
                    ExpiredTime = model.ExpiredTime,
                    Balance = Convert.ToInt32(model.Balance * 100),
                    OperatorID = ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName,
                    blSubsidyCard = model.blSubsidyCard,
                    BTMoney = model.BTMoney,
                    BTSX = model.BTSX
                };
                result = perinfo.SaveGrantCardTemp(per);
            }
            return Json(result);
        }

        /// <summary>
        /// 卡序列号验证
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckCardSerial(Int64 CardSerial)
        {
            bool istongguo = perinfo.CheckCardSerial(CardSerial);
            return Json(istongguo);
        }

        /// <summary>
        /// 人员编号验证
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckEmpNo(int EmpNo)
        {
            bool istongguo = perinfo.CheckEmpNo(EmpNo);
            return Json(istongguo);
        }
        /// <summary>
        /// 单位名称不能为空验证
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckDep_ID(int Dep_ID)
        {
            bool istongguo;
            if (Dep_ID == -1)
            {
                istongguo = false;
            }
            else
            {
                istongguo = true;
            }
            return Json(istongguo);
        }

        public ActionResult CheckCardSerial1(Int64 CardSerial)
        {
            bool bo;
            if (CardSerial == 0)
            {
                bo = true;
            }
            else
            {
                bo = false;
            }
            return Json(bo);
        }
    }
}
